<?php
/**
 * XSLT-Plugin: Renders xml blocks transforming them to XHTML
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Juergen A. Lamers <jaloma.ac@googlemail.com>
 */

if (!defined('DOKU_INC'))define('DOKU_INC', realpath(dirname( __FILE__ ).'/../../../../').'/');
if (!defined('DOKU_PLUGIN'))define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
require_once (DOKU_PLUGIN.'xsltproc/syntax/xsltproc.php');
require_once (DOKU_PLUGIN.'xsltproc/xslt_helper.php');

class syntax_plugin_xsltproc_ixsltproc extends syntax_plugin_xsltproc_xsltproc
{

    /**
     * General Info
     *
     * Returns an associative array with some info about the plugin
     */
    function getInfo()
    {
        return array (
        'author'=>'Juergen A.Lamers',
        'email'=>'jaloma.ac@gogglemail.com',
        'date'=>'2009-03-09',
        'name'=>'ixsltproc Plugin',
        'desc'=>'Allows inline xml markup and render it as XHTML using an external Stylesheet',
        'url'=>'http://www.dokuwiki.org/plugin:xsltproc',
        'url'=>'http://dokuwiki.ich-bin-am-wandern-gewesen.de/plugin:xsltsuite',
        );
    }

    /**
     * Sets syntax type as protected.
     */
    function getType()
    {
        return 'protected';
    }

    /**
     * Paragraph Type
     *
     * Set as 'block': Open paragraphs need to be closed before plugin output
     *
     * @see Doku_Handler_Block
     */
    function getPType()
    {
        return 'block';
    }

    /**
     * Where to sort in?
     */
    function getSort()
    {
        return 200;
    }

    /**
     * Connect pattern to lexer
     */

    function connectTo($mode)
    {
        $this->Lexer->addEntryPattern('<'.$this->getTagName().'>', $mode, $this->connectName());
    }

    function postConnect()
    {
        $this->Lexer->addExitPattern('</'.$this->getTagName().'>', $this->connectName());
    }

    function matchLength()
    {
        return strlen('<'.$this->getTagName());
    }
	
    function getTagName()
    {
        return "ixsltproc";
    }

	function connectName() {
		return "plugin_xsltproc_".$this->getTagName();
	}

    /**
     * Handler to prepare matched data for the rendering process
     *
     * @param   $match   string    The text matched by the patterns
     * @param   $state   int       The lexer state for the match
     * @param   $pos     int       The character position of the matched text
     * @param   $handler ref       Reference to the Doku_Handler object
     * @return  array              Return an array with all data you want to use in render
     */
    function handle($match, $state, $pos, & $handler)
    {
        switch($state)
        {
            case DOKU_LEXER_ENTER:
            case DOKU_LEXER_EXIT:
                return array ($state, '');
                break;
            case DOKU_LEXER_UNMATCHED:
                return array ($state, $match);
                break;
        }

        return array ();
    }

    /**
     * Handles the actual output creation.
     *
     * @param   $format   string   output format to being Rendered
     * @param   $renderer ref      reference to the current renderer object
     * @param   $data     array    data created by handler()
     * @return  boolean            rendered correctly?
     */
    function render($mode, & $renderer, $data)
    {
        if ($mode == 'xhtml')
        {
            list ($state, $match) = $data;
            switch($state)
            {
                case DOKU_LEXER_ENTER:
                    $renderer->doc .= "\n<!-- Begins ".$this->getTagName()." block -->\n";
                    break;
                case DOKU_LEXER_UNMATCHED:
                    $renderer->doc .= $this->transformIXsltProcInXhtml($match);
                    //				$renderer->nocache();
                    break;
                case DOKU_LEXER_EXIT:
                    $renderer->doc .= "\n<!-- Ends ".$this->getTagName()." block -->\n";
                    break;
            }

            return true;
        }
        return false;
    }

    /**
     * Applies the stylesheet defined in settings to the ixsltproc block to get a XHTML document.
     *
     * Uses xsltproc-plugin to do the XSL transformations.
     *
     * @param   $ixsltprocBlock   The giaasset block to be transformed
     * @return  string          The transformed XHTML code
     */
    function transformIXsltProcInXhtml($ixsltprocBlock)
    {
        global $conf;

        $xslDocument = $this->getConf('IXSLTProcDataFile_xslfile');
        $xslDir = realpath(dirname( __FILE__ ).'/../styles');
		return $this->callTransform($ixsltprocBlock, $xslDocument, $xslDir);
    }

	function callTransform($ixsltprocBlock, $xslDocument,$xslDir) {
        $param['xslBaseDir'] = $xslDir.'/';
        return $this->doTransform($ixsltprocBlock, $xslDocument, $param);
	}

    function doTransform($xmlDocument, $xslDocument, $param)
    {
        $txt = "";
        $xHelper = new xslt_helper();
        try
        {
            $xHelper->set_XMLParseMode('mem');
            $resText = $xHelper->renderXML($xmlDocument, $xslDocument, $param);
            if ($resText != null)
            {
                $txt .= $resText;
            }
        } catch(Exception $e)
        {
        	print_r($e);
        }
		return $txt;
    }
}
